﻿using blackhouse.JavaScript.JQuery;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace blackhouse.WebNemo.Web.Controls {

    /// <summary>
    /// Pasek z menu
    /// </summary>
    public class TabMenu : Control {

        #region Fields

        private List<MenuItem> items = new List<MenuItem>();

        #endregion

        #region Properties

        public string CssClass { get; set; }

        #endregion

        #region Methods

        public void AddTabMenuItem(int tabIdx, string name) {
            this.AddTabMenuItem(tabIdx, name, false);
        }

        public void AddTabMenuItem(int tabIdx, string name, bool selected) {
            this.AddMenuItem(tabIdx, name, String.Empty, selected);
        }

        public void AddLinkMenuItem(string name, string url) {
            this.AddLinkMenuItem(name, url, false);
        }

        public void AddLinkMenuItem(string name, string url, bool selected) {
            this.AddMenuItem(null, name, url, selected);
        }

        private void AddMenuItem(int? tabIdx, string name, string url, bool selected) {
            if (tabIdx >= 0 && this.items.Any(mi => mi.Idx == tabIdx)) throw new ArgumentException(String.Format("Element o indeksie {0} został już dodany", tabIdx), "tabIdx");
            MenuItem menuItem = new MenuItem();
            if (tabIdx != null) {
                menuItem.Idx = (int)tabIdx;
                menuItem.IdxSetted = true;
            }
            menuItem.Selected = selected;
            menuItem.Name = name;
            menuItem.Url = url;
            this.items.Add(menuItem);
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            if (!this.DesignMode) {
                JQueryLibraries jql = new JQueryLibraries();
#if DEBUG
                jql.IncludeFileBasedOnJQuery("blackhouse.WebNemo.Web.Scripts.edit-record-tabs.js, blackhouse.WebNemo.Web");
#else
                jql.IncludeFileBasedOnJQuery("blackhouse.WebNemo.Web.Scripts.edit-record-tabs.min.js, blackhouse.WebNemo.Web");
#endif
            }
            else {
                this.AddTabMenuItem(0, "Tab 1", true);
                this.AddTabMenuItem(1, "Tab 2", false);
                this.AddTabMenuItem(2, "Tab 3", false);
            }
        }

        protected override void Render(HtmlTextWriter writer) {
            writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
            string css = "";
            if (String.IsNullOrEmpty(this.CssClass))
                css = "tabs";
            else {
                List<string> cssParts = new List<string>(this.CssClass.Split(' '));
                if (!cssParts.Contains("tabs"))
                    cssParts.Insert(0, "tabs");
                css = String.Join(" ", cssParts.ToArray());
            }
            writer.AddAttribute(HtmlTextWriterAttribute.Class, css);
            if (this.items.Any(mi => mi.IdxSetted))
                writer.AddAttribute("data-panel", "tabs");
            writer.RenderBeginTag(HtmlTextWriterTag.Ul);
            foreach (MenuItem item in this.items) {
                bool renderLink = !String.IsNullOrEmpty(item.Url);
                if (item.IdxSetted)
                    writer.AddAttribute("tab-id", item.Idx.ToString());
                if (item.Selected)
                    writer.AddAttribute("class", "selected");
                writer.RenderBeginTag(HtmlTextWriterTag.Li);
                if (renderLink) {
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, item.Url);
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                }
                writer.Write(item.Name);
                if (renderLink)
                    writer.RenderEndTag();
                writer.RenderEndTag();
            }
            writer.RenderEndTag();
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "space-20");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderEndTag();
        }

        #endregion

        #region Inner

        private struct MenuItem {

            public int Idx;
            public bool IdxSetted;
            public bool Selected;
            public string Name;
            public string Url;

        }

        #endregion

    }

}